<!DOCTYPE html>

<html>
<head>
  <title>JMPromise</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="Array.coffee.html">
                Array.coffee.md
              </a>
            
              
              <a class="source" href="BareObject.coffee.html">
                BareObject.coffee.md
              </a>
            
              
              <a class="source" href="Boolean.coffee.html">
                Boolean.coffee.md
              </a>
            
              
              <a class="source" href="Function.coffee.html">
                Function.coffee.md
              </a>
            
              
              <a class="source" href="Math.coffee.html">
                Math.coffee.md
              </a>
            
              
              <a class="source" href="Maybe.coffee.html">
                Maybe.coffee.md
              </a>
            
              
              <a class="source" href="Nullable.coffee.html">
                Nullable.coffee.md
              </a>
            
              
              <a class="source" href="Number.coffee.html">
                Number.coffee.md
              </a>
            
              
              <a class="source" href="Object.coffee.html">
                Object.coffee.md
              </a>
            
              
              <a class="source" href="Promise.coffee.html">
                Promise.coffee.md
              </a>
            
              
              <a class="source" href="String.coffee.html">
                String.coffee.md
              </a>
            
              
              <a class="source" href="Type.coffee.html">
                Type.coffee.md
              </a>
            
              
              <a class="source" href="header.coffee.html">
                header.coffee.md
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h1 id="jmpromise">JMPromise</h1>

            </div>
            
            <div class="content"><div class='highlight'><pre>	
	<span class="hljs-built_in">window</span>.JMPromise = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">JMPromise</span></span>
	
		[PENDING,FULFILLED,REJECTED] = [<span class="hljs-number">1.</span><span class="hljs-number">.3</span>]
	
		<span class="hljs-function"><span class="hljs-title">delay</span> = <span class="hljs-params">(fn)</span> -&gt;</span> setTimeout fn, <span class="hljs-number">0</span>
	
		<span class="hljs-function"><span class="hljs-title">chain</span> = <span class="hljs-params">(promise,fn,value)</span> -&gt;</span> delay<span class="hljs-function"> -&gt;</span>
			<span class="hljs-keyword">try</span>
				ret = fn.call value[<span class="hljs-number">0</span>], value...
				promise.fulfil ret
			<span class="hljs-keyword">catch</span> reason
				promise.reject reason
		
		<span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span>
		
			<span class="hljs-property">@status</span> = PENDING
			<span class="hljs-property">@value</span>  = <span class="hljs-literal">undefined</span>
			<span class="hljs-property">@reason</span> = <span class="hljs-literal">undefined</span>
	
			<span class="hljs-property">@waiting</span> = []
		
			<span class="hljs-property">@fulfil</span> args... <span class="hljs-keyword">if</span> args.length &gt; <span class="hljs-number">0</span>
	
		<span class="hljs-attribute">then</span>: <span class="hljs-function"><span class="hljs-params">(fulfilled,rejected)</span> -&gt;</span>
		
			promise = <span class="hljs-keyword">new</span> <span class="hljs-property">@constructor</span>()
		
			<span class="hljs-keyword">if</span> <span class="hljs-keyword">typeof</span> fulfilled != <span class="hljs-string">'function'</span> <span class="hljs-keyword">then</span> fulfilled = Function.arguments
			<span class="hljs-keyword">if</span> <span class="hljs-keyword">typeof</span> rejected != <span class="hljs-string">'function'</span> <span class="hljs-keyword">then</span> rejected = Function.identity
			
			<span class="hljs-keyword">switch</span> <span class="hljs-property">@status</span>
				<span class="hljs-keyword">when</span> PENDING
					<span class="hljs-property">@waiting</span>.push
						<span class="hljs-attribute">promise</span>: promise
						<span class="hljs-attribute">fulfilled</span>: fulfilled
						<span class="hljs-attribute">rejected</span>: rejected
				<span class="hljs-keyword">when</span> FULFILLED
					chain promise, fulfilled, <span class="hljs-property">@value</span>
				<span class="hljs-keyword">when</span> REJECTED
					chain promise, rejected, [<span class="hljs-property">@reason</span>]
				
			<span class="hljs-keyword">return</span> promise</pre></div></div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>Tests: full</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>		<span class="hljs-attribute">fulfil</span>: Function.Requiring(<span class="hljs-function">-&gt;</span> <span class="hljs-property">@status</span> == PENDING) \
			<span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@value</span>...)</span> -&gt;</span>
				<span class="hljs-property">@status</span> = FULFILLED
				<span class="hljs-keyword">for</span> {promise,fulfilled} <span class="hljs-keyword">in</span> <span class="hljs-property">@waiting</span>
					chain promise, fulfilled, <span class="hljs-property">@value</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Tests: full    </p>

            </div>
            
            <div class="content"><div class='highlight'><pre>		<span class="hljs-attribute">reject</span>: Function.Requiring(<span class="hljs-function">-&gt;</span> <span class="hljs-property">@status</span> == PENDING) \
			<span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@reason</span>)</span> -&gt;</span>
				<span class="hljs-property">@status</span> = REJECTED
				<span class="hljs-keyword">for</span> {promise,rejected} <span class="hljs-keyword">in</span> <span class="hljs-property">@waiting</span>
					chain promise, rejected, [<span class="hljs-property">@reason</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <h1 id="pre-resolved-jmpromises">Pre-resolved JMPromises</h1>

            </div>
            
            <div class="content"><div class='highlight'><pre>		
		<span class="hljs-property">@Fulfilled</span>: Function.Returning(<span class="hljs-function">-&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">this</span> ) \
			<span class="hljs-function"><span class="hljs-params">(promise)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span> promise.fulfil args...
			
		<span class="hljs-property">@Rejected</span>: Function.Returning(<span class="hljs-function">-&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">this</span> ) \
			<span class="hljs-function"><span class="hljs-params">(promise)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(args...)</span> -&gt;</span> promise.reject args...</pre></div></div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <h1 id="typed-jmpromises">Typed JMPromises</h1>

            </div>
            
            <div class="content"><div class='highlight'><pre>			
		<span class="hljs-property">@Of</span>: Function.Cache.From<span class="hljs-function"><span class="hljs-params">(Function)</span>.<span class="hljs-title">To</span><span class="hljs-params">(Function)</span> <span class="hljs-params">(constructor)</span> -&gt;</span>
			<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">this</span></span>
				<span class="hljs-attribute">fulfil</span>: Function.Of(constructor) <span class="hljs-attribute">this</span>::fulfil</pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <h1 id="jmpromise-combinators">JMPromise combinators</h1>

            </div>
            
            <div class="content"><div class='highlight'><pre>			
		<span class="hljs-property">@conjoin</span>: Function.From([JMPromise]).Returning(<span class="hljs-function">-&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">this</span>) \
			<span class="hljs-function"><span class="hljs-params">(conjunction)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(promises...)</span> -&gt;</span>
				
				<span class="hljs-function"><span class="hljs-title">completed</span> = -&gt;</span>
					<span class="hljs-keyword">if</span> promises.none(<span class="hljs-function"> -&gt;</span> <span class="hljs-property">@status</span> == PENDING )
						conjunction.fulfil promises.map<span class="hljs-function"><span class="hljs-params">( ({value:[ret]}) -&gt; ret)</span>...
				
				<span class="hljs-title">promises</span>.<span class="hljs-title">each</span> <span class="hljs-params">(promise)</span> -&gt;</span> <span class="hljs-property">@then</span> completed, completed
					
				
		
		<span class="hljs-property">@disjoin</span>: Function.From([JMPromise]).Returning(<span class="hljs-function">-&gt;</span> <span class="hljs-keyword">new</span> <span class="hljs-keyword">this</span> ) \
			<span class="hljs-function"><span class="hljs-params">(disjunction)</span> -&gt;</span> <span class="hljs-function"><span class="hljs-params">(promises...)</span> -&gt;</span>
				[fulfil,reject] = Function.delegates<span class="hljs-function"> -&gt;</span> [ disjunction, disjunction.fulfil, disjunction.reject ]
				promises.each<span class="hljs-function"> -&gt;</span> <span class="hljs-property">@then</span> fulfil, reject</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
